package com.yvan.periscope.domain;


import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

@Getter
@Setter
public class Resource implements Serializable {

    private static final long serialVersionUID = -6850476779988485919L;

    @JsonProperty("id")
    private String resourceId;

    @JsonProperty("name")
    private String resourceName;

    @JsonProperty("icon")
    private String icon;

    @JsonProperty("url")
    private String resourceUrl;

    @JsonProperty("childs")
    private List<Resource> childs;

    public static boolean isEqualPath(String s1, String s2) {
        int p1 = s1.indexOf("?");
        if (p1 >= 0) {
            s1 = s1.substring(0, p1);
        }
        int p2 = s2.indexOf("?");
        if (p2 >= 0) {
            s2 = s2.substring(0, p1);
        }
        return s1.toLowerCase().equals(s2.toLowerCase());
    }

    public boolean childContains(String url) {
        if (childs != null && !childs.isEmpty()) {
            for (Resource cr : childs) {
                if (isEqualPath(cr.resourceUrl, url)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isEqualPath(String s1) {
        return isEqualPath(this.resourceUrl, s1);
    }
}

